﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Tools.Ribbon;

namespace MailAnalyse
{
    public partial class MenuRibbon
    {
        private void CreatePTButton_Click(object sender, RibbonControlEventArgs e)
        {
            Globals.ThisAddIn.Application.AdvancedSearch("'InBox'", "urn:schemas:mailheader:subject = 'CreateFuillmentOrderFailMail'", false, "Test");
            Globals.ThisAddIn.Application.AdvancedSearch("'InBox'", "urn:schemas:mailheader:subject = 'ProcessOrderFailMail'", false, "Test1");

            Globals.ThisAddIn.Application.AdvancedSearchComplete += Application_AdvancedSearchComplete;

           
            while (log1 == null || log2 == null)
                System.Windows.Forms.Application.DoEvents();
            {
                StringBuilder stringBuilder = new StringBuilder();

                Dictionary<string ,string> errors=new Dictionary<string, string>();

                foreach (LogItem logItem in log2)
                {
                    if(!errors.ContainsKey(logItem.Pt)) errors.Add(logItem.Pt,logItem.Exception);
                }
                foreach (KeyValuePair<string, string> logItem in errors)
                {
                    KeyValuePair<string, string> item = logItem;
                    List<LogItem> logItems = log1.FindAll(current => current.Pt == item.Key);

                    stringBuilder.AppendFormat("{0},\"{1}\",\"{2}\"\r\n", logItem.Key, logItem.Value, logItems.Count > 0 ? logItems[0].Exception : String.Empty);

                    for (int i = 1; i < logItems.Count; i++)
                    {
                        stringBuilder.AppendFormat("{0},\"{1}\",\"{2}\"\r\n", string.Empty, string.Empty, logItems[i].Exception);

                    }
                }
                File.WriteAllText("D:\\xx.csv", stringBuilder.ToString());
            }
        }

        Regex rx = new Regex(@"PickTicketNumber:(?<PT>[^\r\n]*)\r\nExceptionInfo:(?<Exception>[^\r\n]*)");

        private List<LogItem> log1;
        private List<LogItem> log2;

        private void Application_AdvancedSearchComplete(Search searchResult)
        {
            List<LogItem> log = new List<LogItem>();
            for (int i = 1; i < searchResult.Results.Count; i++)
            {
                MailItem xx = (MailItem)searchResult.Results[i];
                MatchCollection matchCollection = rx.Matches(xx.Body);

                log.AddRange(from Match match in matchCollection
                             select new LogItem(match.Groups["PT"].Value, match.Groups["Exception"].Value));
            }

            if (searchResult.Tag == "Test") log1 = log;
            else log2 = log;
        }

        private class LogItem
        {
            private string pt;
            private string exception;

            public LogItem(string pt, string exception)
            {
                this.pt = pt;
                this.exception = exception;
            }

            public string Pt
            {
                get { return pt; }
            }

            public string Exception
            {
                get { return exception; }
            }
        }
    }
}
